package com.ht.mt.sric.managed.gestion.tramo;

import com.ht.mt.sric.dto.TramoDto;
import com.ht.mt.sric.entity.Tramo;
import com.ht.mt.sric.sesion.remota.TramoSessionEJBBeanRemote;
import com.ht.mt.sric.util.ServiceLocator;
import com.ht.mt.sric.util.UConstantes;

import com.ht.mt.sric.util.UParametros;
import com.ht.mt.sric.util.UProperties;

import com.ht.mt.sric.utiles.FacesUtil;

import java.util.HashMap;

import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import oracle.adf.view.rich.context.AdfFacesContext;

@ManagedBean(name = "amtramoMB")
@RequestScoped
public class AMTramoMB {

    private ServiceLocator sLocator;
    private TramoSessionEJBBeanRemote ejbTramo;
    private Integer typeWindows;
    private String nameWindows;
    private TramoDto tramo;

    private UIViewRoot dialog;
    private ViewHandler viewHandler;
    private AdfFacesContext adfContext;
    private HashMap windowProperties;
    private HashMap dialogParameters;
    private FacesContext facesContext;
    
    private boolean showButton;
    private boolean readOnly;

    public AMTramoMB() {
        super();
        try {
            sLocator = new ServiceLocator();
            ejbTramo =(TramoSessionEJBBeanRemote) sLocator.findServiceRemote(UProperties.getMessage(UParametros.RUTA_JNDI, "JNDI_TRAMO"));

            windowProperties = new HashMap();
            dialogParameters = new HashMap();
            facesContext = FacesContext.getCurrentInstance();
            adfContext = FacesUtil.getAdfFacesContext();
            viewHandler = facesContext.getApplication().getViewHandler();


            if ((TramoDto) adfContext.getPageFlowScope().get("tramo") != null) {
                tramo = (TramoDto) adfContext.getPageFlowScope().get("tramo");
            }

            if ((Integer) adfContext.getPageFlowScope().get("typeWindows") != null) {
                typeWindows = (Integer) adfContext.getPageFlowScope().get("typeWindows");
                if (typeWindows == UConstantes.ADD) {
                    setNameWindows(UProperties.getMessage(UParametros.RUTA_MENSAJES, "ADD_TRAMO"));
                    setReadOnly(false);
                    setShowButton(true);
                } else if (typeWindows == UConstantes.UPDATE) {
                    setNameWindows(UProperties.getMessage(UParametros.RUTA_MENSAJES, "UPDATE_TRAMO"));
                    setReadOnly(false);
                    setShowButton(true);
                } else if (typeWindows == UConstantes.VIEW) {
                    setNameWindows(UProperties.getMessage(UParametros.RUTA_MENSAJES, "VIEW_TRAMO"));
                    setReadOnly(true);
                    setShowButton(false);
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String saveTramo() {
        Tramo beanTramo = TramoDto.toBeanTramo(tramo);
        if (beanTramo != null) {

            if (typeWindows == UConstantes.ADD) {
                ejbTramo.persistTramo(beanTramo);
            } else if (typeWindows == UConstantes.UPDATE) {

            }
        }

        return "";
    }


    public void setShowButton(boolean showButton) {
        this.showButton = showButton;
    }

    public boolean isShowButton() {
        return showButton;
    }

    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public void setNameWindows(String nameWindows) {
        this.nameWindows = nameWindows;
    }

    public String getNameWindows() {
        return nameWindows;
    }


    public void setTramo(TramoDto tramo) {
        this.tramo = tramo;
    }

    public TramoDto getTramo() {
        return tramo;
    }
}
